import api from "@/api/api";

// 获取图片上传的凭证
const getUploadCredential = () => {
    return api.UploadCredential();
};

// 动态导入 ali-oss 并创建 OSS 客户端
const createOSSClient = async () => {
    if (process.client) {
        try {
            const { default: OSS } = await import("ali-oss");
            const resp = await getUploadCredential();
            console.log(resp, "respresp");
            const data = resp;
            return new OSS({
                region: "oss-cn-beijing",
                accessKeyId: data.credentials.AccessKeyId,
                accessKeySecret: data.credentials.AccessKeySecret,
                bucket: data.bucket,
                host: data.host,
                stsToken: data.credentials.SecurityToken,
                refreshSTSToken: async () => {
                    const refreshToken = await getUploadCredential();
                    const { data } = refreshToken.data;
                    return {
                        accessKeyId: data.credentials.AccessKeyId,
                        accessKeySecret: data.credentials.AccessKeySecret,
                        stsToken: data.credentials.SecurityToken,
                    };
                },
            });
        } catch (error) {
            console.error("Failed to import ali-oss or create OSS client:", error);
            return null;
        }
    }
    return null;
};
export async function csoss(fileName, content) {
    const client = await createOSSClient();
    console.log(client, "clientclientclient");
}
// 上传文件函数
export async function UploadFile(fileName, content) {
    let key = `upload/${new Date().getTime()}/${fileName}`;
    const client = await createOSSClient();
    if (client) {
        try {
            await client.put(key, content);
        } catch (err) {
            console.error("-->putObject", err);
            key = null;
        }
    } else {
        key = null;
    }
    return key;
}

// 上传头像文件函数
export async function AvatarFile(fileName, content) {
    let key = `avatar/${new Date().getTime()}/${fileName}`;
    const client = await createOSSClient();
    if (client) {
        try {
            await client.put(key, content);
        } catch (err) {
            console.error("-->putObject", err);
        }
    } else {
        key = null;
    }
    return key;
}

// 上传任务文件函数
export async function UploadMissionFile(fileName, content) {
    let key = `upload/mission/${fileName}`;
    const client = await createOSSClient();
    if (client) {
        try {
            await client.put(key, content);
        } catch (err) {
            console.error("-->putObject", err);
        }
    } else {
        key = null;
    }
    return key;
}

// 通过文件名称判断类型是否符合
export const checkFileTypeByName = (name, type) => {
    const fileType = name.split(".")?.slice(-1)?.[0];
    if (!name || !type) {
        return false;
    }
    if (Array.isArray(type)) {
        return type.includes(fileType);
    }
    if (typeof type === "string") {
        return fileType === type;
    }
    return false;
};
